package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.domain.entity.SysDept;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.system.domain.dto.HomePoReqDTO;
import com.zhuoyue.system.domain.po.TblHomePo;
import com.zhuoyue.system.domain.vo.TblHomePositionVo;
import com.zhuoyue.system.domain.vo.TblHomeVo;
import com.zhuoyue.system.mapper.SysDeptMapper;
import com.zhuoyue.system.mapper.TblHomeMapper;
import com.zhuoyue.system.service.ITblHomeService;
import com.zhuoyue.web.utils.OrgCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 家庭Controller
 *
 * @author hewenze
 * @date 2020-08-07
 */
@RestController
@Api(value = "家庭接口", tags = "家庭接口")
@RequestMapping("/system/home")
public class TblHomeController extends BaseController {
    @Autowired
    private ITblHomeService tblHomeService;

    @Autowired
    private TblHomeMapper tblHomeMapper;

    @Autowired
    private OrgCodeUtils orgCodeUtils;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询家庭列表
     */
    @ApiOperation(value = "查询家庭列表", notes = "查询家庭列表")
    @GetMapping("/list")
    public TableDataInfo list(HomePoReqDTO homePoReq, HttpServletRequest request) {
        if (homePoReq.getDeptId() == null) {
            homePoReq.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        } else {
            SysDept sysDept = sysDeptMapper.selectDeptById(homePoReq.getDeptId());
            homePoReq.setOrganizationCode(sysDept.getOrgCode());
        }
        startPage();
        TblHomePo tblHomePo = new TblHomePo().dtoToDo(homePoReq);
        List<TblHomeVo> list = tblHomeService.selectTblHomeList(tblHomePo);
        return getDataTable(list);
    }


    /**
     * 查询家庭位置信息列表
     */
    @ApiOperation(value = "查询家庭列表", notes = "查询家庭列表")
    @GetMapping("/listPosition")
    public TableDataInfo listPosition(TblHomePo tblHomePo, HttpServletRequest request) {

        tblHomePo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        startPage();
        List<TblHomePositionVo> list = tblHomeService.selectTblHomePositionList(tblHomePo);
        return getDataTable(list);
    }


    /**
     * 导出家庭列表
     */
    @ApiOperation(value = "导出家庭列表", notes = "导出设备列表")
    @Log(title = "家庭", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TblHomePo tblHomePo, HttpServletRequest request) {
        tblHomePo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        tblHomePo.setPageNum(null);
        tblHomePo.setPageSize(null);
        List<TblHomeVo> list = tblHomeService.selectTblHomeList(tblHomePo);
        ExcelUtil<TblHomeVo> util = new ExcelUtil<TblHomeVo>(TblHomeVo.class);
        return util.exportExcel(list, "家庭");
    }

    /**
     * 获取网关数量
     */
    @ApiOperation(value = "查询网关数量", notes = "查询网关数量")
    @GetMapping(value = "/getHomeCount")
    public AjaxResult getHomeCount(TblHomePo tblHomePo, HttpServletRequest request) {
        return AjaxResult.success(tblHomeService.getHomeCount(tblHomePo));
    }

    @GetMapping("/getHomeByAccountAndOrgCode")
    public TableDataInfo getHomeByAccountAndOrgCode(String account, String orgCode, String homeName) {
        return getDataTable(tblHomeMapper.getHomeByAccountAndOrgCode(account, orgCode, homeName));
    }

    /**
     * 修改家庭的酒店排序
     *
     * @return
     */
    @PutMapping("/updateHomeHotelOrderNum")
    public AjaxResult updateHomeHotelOrderNum(String homeId, Integer orderNum) {
        if (orderNum != null) {
            tblHomeMapper.updateHomeHotelOrderNum(homeId, orderNum);
        }
        return AjaxResult.success();
    }
}
